#include "file.h"
extern char file_name[128];
void tra(FILE *file) // 对文件进行遍历的函数
{
    S sst[100] = {};
    file = fopen(file_name, "r");
    for (int i = 0; i < 7; i++)
    {
        fscanf(file, "名字：%s\t性别:%s\t年龄:%d\t学号:%d\t成绩:%d\n",
               sst[i].name, sst[i].sex, &sst[i].age,
               &sst[i].id, &sst[i].sco);
    }
    for (int i = 0; i < 7; i++)
    {
        printf("名字：%s\t性别:%s\t年龄:%d\t学号:%d\t成绩:%d\n",
               sst[i].name, sst[i].sex, sst[i].age,
               sst[i].id, sst[i].sco);
    }
    fclose(file);
    file = NULL;
}

void chc(FILE *file, S *stu, char len)          // 尾插，直接从尾部插入 定义文件指针，需加入的结构体,len为需要插入多少行
{
    file = fopen(file_name, "a+");
    for (int i = 0; i < len; i++)
    {
        fprintf(file, "名字：%s\t性别:%s\t年龄:%d\t学号:%d\t成绩:%d\n",
                stu[i].name, stu[i].sex, stu[i].age,
                stu[i].id, stu[i].sco);
    }
    fclose(file);
    file = NULL;
}

void del(FILE *file)                            //尾删 固定只保有前五行
{
    S stu[100] = {};
    file = fopen(file_name, "r");
    for (int i = 0; i < 7; i++)
    {
        fscanf(file, "名字：%s\t性别:%s\t年龄:%d\t学号:%d\t成绩:%d\n",
               stu[i].name, stu[i].sex, &stu[i].age,
               &stu[i].id, &stu[i].sco);
    }
    fclose(file);
    file = NULL;
    file = fopen(file_name, "w+");
    for (int i = 0; i < 5; i++)
    {
        fprintf(file, "名字：%s\t性别:%s\t年龄:%d\t学号:%d\t成绩:%d\n",
                stu[i].name, stu[i].sex, stu[i].age,
                stu[i].id, stu[i].sco);
    }
    fclose(file);
    file = NULL;
}

void fin(FILE *file)                            //直接使用此函数可进行前六行的成绩更改
{
    printf("请输入需要更改成绩的学生：\n");
    char name[120] = "";
    scanf("%s",name);
    S stu[100] = {};
    printf("请输入需要更改该学生成绩为：\n");
    int j;
    scanf("%d",&j);
    file = fopen(file_name, "r");
    for (int i = 0; i < 6; i++)
    {
        fscanf(file, "名字：%s\t性别:%s\t年龄:%d\t学号:%d\t成绩:%d\n",
               stu[i].name, stu[i].sex, &stu[i].age,
               &stu[i].id, &stu[i].sco);
    }
    fclose(file);
    file = NULL;
     file = fopen(file_name, "w+");
    for (int i = 0; i < 6; i++)
    {
         if (!strcmp(stu[i].name,name))
        {
            fprintf(file, "名字：%s\t性别:%s\t年龄:%d\t学号:%d\t成绩:%d\n",
                stu[i].name, stu[i].sex, stu[i].age,
                stu[i].id, j);
                continue;
        }
        fprintf(file, "名字：%s\t性别:%s\t年龄:%d\t学号:%d\t成绩:%d\n",
                stu[i].name, stu[i].sex, stu[i].age,
                stu[i].id, stu[i].sco);
    }
    fclose(file);
    file = NULL;
}